package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Course;
import com.xmy.cultivate.entity.EnrollCustomer;
import com.xmy.cultivate.entity.OrderApply;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.views.ResTransaction;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单报名信息 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-07-28
 */
public interface OrderApplyMapper extends BaseMapper<OrderApply> {


    @Select("SELECT * FROM order_view ${ew.customSqlSegment}")
    public abstract IPage<OrderApply> findAll(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 欠费列表
     * @param wrapper
     * @return
     */
    @Select("select id,getDictName(order_type, 13) as orderTypeName,IFNULL(handle_person_name,'') as handlePersonName,handle_date as handleDate,getSchoolName(handle_school_id) as handleSchoolName,own_price as ownPrice from order_apply ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "course",
                    column = "id",
                    many = @Many(select = "com.xmy.cultivate.mapper.OrderApplyMapper.getOrderDetailListForOrderId")
            )
    })
    public abstract List<Map<String,Object>> orderArrears(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select id,course_name from order_detail where order_id = #{orderId}")
    public abstract List<Map<String,Object>> getOrderDetailListForOrderId(Long orderId);


    @Select("select * from expenses_and_receipts_view ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "orderPayAccountList",
                    column = "id",
                    many = @Many(select = "com.xmy.cultivate.mapper.OrderPayAccountMapper.getListForOrderId")
            )
    })
    public abstract IPage<OrderApply> expensesAndReceipts(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);


    @Select("select * from transaction_view ${ew.customSqlSegment}")
    @Results({
            @Result(column = "order_id",property = "order_id"),
            @Result(
                    property = "orderPayAccountList",
                    column = "order_id",
                    many = @Many(select = "com.xmy.cultivate.mapper.OrderPayAccountMapper.getListForOrderId")
            )
    })
    public abstract IPage<ResTransaction> transaction(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select * from affirm_income_view ${ew.customSqlSegment}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(
                    property = "orderPayAccountList",
                    column = "id",
                    many = @Many(select = "com.xmy.cultivate.mapper.OrderPayAccountMapper.getListForOrderId")
            ),
            @Result(
                    property = "course",
                    column = "id",
                    many = @Many(select = "com.xmy.cultivate.mapper.OrderApplyMapper.getOrderDetailListForOrderId")
            )
    })
    public abstract IPage<OrderApply> affirmIncome(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);


    @Select({"<script>",
            "SELECT",
                    "count(t1.id)",
                    "FROM",
                    "order_apply AS t1",
                    "INNER JOIN student AS t2 ON t1.student_id = t2.id ",
                    "WHERE",
                    "t1.deleted = 0 ",
                    "AND t1.`status` &lt;&gt; 2 ",
                    "AND t1.year_part = ${yearPart} ",
                    "AND t1.quarter_num = ${quarterNum} ",
                    "AND t1.order_type = 1 ",
                    "AND t1.created_at BETWEEN #{startDateTime} ",
                    "AND #{endDateTime} ",
                    "AND t2.linkman_phone = #{phone}",
            "</script>"})
    public Integer getOrderApplyByDateAndStudentId(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,  @Param("startDateTime") String startDateTime,@Param("endDateTime") String endDateTime,@Param("phone") String phone);

}
